<?php
defined('SYSTEM_PATH') or die('Restricted Access');

class System extends Loader
{
    private $onError = FALSE;

    function __construct()
    {
        parent::__construct();
        global $_app_config;
        global $_system_config;
        $this->autoLoad($_system_config['autoLoad']);
        if (isset($_app_config['autoLoad']) && count($_app_config['autoLoad']) > 0)
        {
            $this->autoLoad($_app_config['autoLoad']);
        }
    }

    public function process()
    {
        global $_system_config;
        global $_app_config;
        if (!class_exists('URI'))
        {
            exit('Problem detected, no URI class defined !');
        }

        $this->URI = new Uri();
        switch ($this->URI->getRequestType())
        {
            case 0:
                {
                $controller = (isset($_app_config['default_controller']) && strlen($_app_config['default_controller'])) ? $_app_config['default_controller'] : $_system_config['default_controller'];
                $method = (isset($_app_config['default_method']) && strlen($_app_config['default_method'])) ? $_app_config['default_method'] : $_system_config['default_method'];
                break;
                }

            case 1:
                {
                $controller = $this->URI->getController();
                $method = (isset($_app_config['default_method']) && strlen($_app_config['default_method'])) ? $_app_config['default_method'] : $_system_config['default_method'];
                break;
                }


            case 2:
            case 3:
                {
                $controller = $this->URI->getController();
                $method = $this->URI->getMethod();
                break;
                }
        }
        $param = $this->URI->getParams();
        $this->loadController($controller);
        if (method_exists($this->CONTROLLER, $method))
        {
            call_user_func_array(array($this->CONTROLLER, $method), $param);
        }
        else
        {
            $method = 'index';
            $this->loadController($_app_config['404page']);
            if (isset($_app_config['component_enable']) && $_app_config['component_enable'] == TRUE)
            {
                $_app_config['component_enable'] = FALSE;
            }
            call_user_func_array(array($this->CONTROLLER, $method), $param);
        }
    }

    private function autoLoad($array)
    {
        foreach ($array as $key => $value)
        {
            $path = (($value['system'] == TRUE) ? SYSTEM_PATH : APP_PATH) . $value['dir'] . DS . $key . '.php';
            $this->loadClass($key, $path);
        }
    }

    private function loadController($controllerName)
    {

        $controllerName = strtolower($controllerName);
        $controllerClass = $controllerName . 'Controller';
        $path = APP_PATH . 'controllers' . DS . $controllerName . '.controller.php';
        if (!$this->loadClass($controllerClass, $path, FALSE))
        {
            $path = APP_PATH . 'components' . DS . $controllerName . DS . 'controller' . DS . 'default.controller.php';
            if ($this->loadClass($controllerClass, $path, FALSE))
            {
                global $_app_config;
                $_app_config['component_enable'] = TRUE;
                $_app_config['component_name'] = $controllerName;
            }
            else
            {
                global $_app_config;
                if (isset($_app_config['404page']))
                {
                    $controllerClass = $_app_config['404page'] . 'Controller';
                    $path = APP_PATH . 'controllers' . DS . $_app_config['404page'] . '.controller.php';
                    if ($this->loadClass($controllerClass, $path, FALSE))
                    {
                        $this->CONTROLLER = new $controllerClass;
                        $this->onError = TRUE;
                        return;
                    }
                }
                exit("Controller $controllerName does not exist!");
            }

        }
        $this->CONTROLLER = new $controllerClass;
    }
}

?>
